nslookup -type=MX someschool.eduFor the response to this command, there may be several mail servers that will deliver mail to mailboxes in the domain someschool.edu. Suppose that the name of one of them is mx1.someschool.edu. In this case, the following command will establish a TCP connection to this mail server. (Notice that the port number 25 is specified on the command line.)
telnet mx1.someschol.edu 25At this point, the telnet program will allow you to enter SMTP commands, and will display the responses from the mail server. For example, the following sequence of commands would send email to bob from alice
HELO alice MAIL FROM: <[email protected]> RCPT TO: <[email protected]> DATA SUBJECT: hello Hi Bob, How's the weather? Alice. . QUITThe SMTP protocol was originally designed to allow people to manually interact with mail servers in a conversational manner. For this reason, if you enter a command with incorrect syntax, or with unacceptable arguments, the server will return a message stating this, and will allow you to try again.
java EmailSenderThis means you will include in your code the details of the particular email message you are trying to send.
Here is a skeleton of the code you'll need to write:
import java.io.*; import java.net.*; public class EmailSender { public static void main(String[] args) throws Exception { // Establish a TCP connection with the mail server. // Create a BufferedReader to read a line at a time. InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); // Read greeting from the server. String response = br.readLine(); System.out.println(response); if (!response.startsWith("220")) { throw new Exception("220 reply not received from server."); } // Get a reference to the socket's output stream. OutputStream os = socket.getOutputStream(); // Send HELO command and get server response. String command = "HELO alice\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server."); } // Send MAIL FROM command. // Send RCPT TO command. // Send DATA command. // Send message data.
// End with line with a single period. // Send QUIT command. } }